//
//  KeyFrameAnimationView.m
//  AnimationDemo
//
//  Created by Jiaozl 2018 on 2018/9/11.
//  Copyright © 2018年 Jiaozl. All rights reserved.
//

#import "KeyFrameAnimationView.h"

@implementation KeyFrameAnimationView


- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    [self pathAnimationLayer2];
        
    [self initRectLayer];
}

- (void)pauseRectRunLayer {
    [self pauseLayer: self.layer.sublayers.lastObject];
}

- (void)resumeRectRunLayer {
    [self resumeLayer: self.layer.sublayers.lastObject];
}


#pragma mark - 暂停与重启动画
-(void)pauseLayer:(CALayer*)layer {
    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
    layer.speed = 0.0;
    layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer {
    CFTimeInterval pausedTime = [layer timeOffset];
    layer.speed = 1.0;
    layer.timeOffset = 0.0;
    layer.beginTime = 0.0;
    
    // 获取图层当前时间
    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
    layer.beginTime = timeSincePause;
}


#pragma mark - CAKeyFrameAnimation
/** 绘制一条路径，产生一个帧动画 */
- (void)pathAnimationLayer {
    
    // 添加图层
    UIView *objView = [[UIView alloc] initWithFrame:CGRectMake(60, 200, 30, 30)];
    objView.backgroundColor = UIColor.redColor;
    [self addSubview:objView];
    
    // 设定动画路径
    UIBezierPath *ellipsePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(75, 120, 200, 300)];
    //    CGMutablePathRef ellipsePath = CGPathCreateMutable();
    //    CGPathMoveToPoint(ellipsePath, NULL, (250-75)/2.0, 120);
    //    CGPathAddEllipseInRect(ellipsePath, NULL, CGRectMake(75, 120, 200, 300));
    
    // 绘制路径
    CAShapeLayer *shapelayer = [[CAShapeLayer alloc] init];
    shapelayer.strokeColor = UIColor.redColor.CGColor;
    shapelayer.fillColor = UIColor.clearColor.CGColor;
    shapelayer.lineWidth = 0.5;
    shapelayer.lineJoin = kCALineJoinRound;
    shapelayer.lineCap = kCALineCapRound;
    shapelayer.path = ellipsePath.CGPath;
    [self.layer addSublayer:shapelayer];
    
    // 帧动画
    CAKeyframeAnimation *keyframeAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyframeAni.path = ellipsePath.CGPath;
    keyframeAni.duration = 4.0;
    keyframeAni.repeatCount = MAXFLOAT;
    keyframeAni.removedOnCompletion = NO;
    keyframeAni.calculationMode = kCAAnimationPaced;
    keyframeAni.fillMode = kCAFillModeForwards;
    keyframeAni.rotationMode = kCAAnimationRotateAutoReverse; // 伴随着在路径上的运动，随时调整自己的角度
    
    [objView.layer addAnimation:keyframeAni forKey:@"pathAnim"];
}
/** 绘制一条路径，产生一个帧动画 */
- (void)pathAnimationLayer2 {
    
    // 添加图层
    CALayer *objlayer = [[CALayer alloc] init];
    objlayer.backgroundColor = UIColor.redColor.CGColor;
    objlayer.frame = CGRectMake(60, 200, 30, 30);
    //    objlayer.cornerRadius =  15;
    [self.layer addSublayer:objlayer];
    
    // 绘制路径
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddEllipseInRect(context, CGRectMake(75, 120, 200, 300));
    CGContextStrokePath(context);
    
    // 设定动画路径
    CGMutablePathRef ellipsePath = CGPathCreateMutable();
    CGPathMoveToPoint(ellipsePath, NULL, (250-75)/2.0, 120);
    CGPathAddEllipseInRect(ellipsePath, NULL, CGRectMake(75, 120, 200, 300));
    
    // 帧动画
    CAKeyframeAnimation *keyframeAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyframeAni.path = ellipsePath;
    keyframeAni.duration = 4.0;
    keyframeAni.repeatCount = MAXFLOAT;
    keyframeAni.removedOnCompletion = NO;
    keyframeAni.calculationMode = kCAAnimationPaced;
    keyframeAni.fillMode = kCAFillModeForwards;
    keyframeAni.rotationMode = kCAAnimationRotateAutoReverse; // 伴随着在路径上的运动，随时调整自己的角度
    CGPathRelease(ellipsePath);
    
    
    [objlayer addAnimation:keyframeAni forKey:@"pathAnim"];
    
}
- (void)initRectLayer {
    // 添加动画层
    CALayer *rectLayer = [[CALayer alloc] init];
    rectLayer.backgroundColor = UIColor.redColor.CGColor;
    rectLayer.frame = CGRectMake(60, 180, 30, 30);
    rectLayer.cornerRadius =  15;
    [self.layer addSublayer:rectLayer];
    
    // 动画
    CAKeyframeAnimation *rectRunAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    //    rectRunAnimation.values = @[
    //                                [NSValue valueWithCGPoint:rectLayer.frame.origin],
    //                                [NSValue valueWithCGPoint:CGPointMake(rectLayer.frame.origin.x+200, rectLayer.frame.origin.y)],
    //                                [NSValue valueWithCGPoint:CGPointMake(rectLayer.frame.origin.x+200, rectLayer.frame.origin.y+100)],
    //                                [NSValue valueWithCGPoint:CGPointMake(rectLayer.frame.origin.x, rectLayer.frame.origin.y+100)],
    //                                [NSValue valueWithCGPoint:rectLayer.frame.origin]
    //                                ];
    
    // 以下设置path代码等同于设置values代码
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, rectLayer.frame.origin.x, rectLayer.frame.origin.y);
    CGPathAddLineToPoint(path, NULL, rectLayer.frame.origin.x+200, rectLayer.frame.origin.y);
    CGPathAddLineToPoint(path, NULL, rectLayer.frame.origin.x+200, rectLayer.frame.origin.y+100);
    CGPathAddLineToPoint(path, NULL, rectLayer.frame.origin.x, rectLayer.frame.origin.y+100);
    CGPathAddLineToPoint(path, NULL, rectLayer.frame.origin.x, rectLayer.frame.origin.y);
    rectRunAnimation.path = path;
    CGPathRelease(path);
    
    
    
    rectRunAnimation.keyTimes = @[
                                  [NSNumber numberWithFloat:0.0],
                                  [NSNumber numberWithFloat:0.6],
                                  [NSNumber numberWithFloat:0.7],
                                  [NSNumber numberWithFloat:0.8],
                                  [NSNumber numberWithFloat:1],
                                  ];
    
    rectRunAnimation.timingFunctions = @[
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                                         [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]
                                         ];
    rectRunAnimation.repeatCount = MAXFLOAT;
    rectRunAnimation.autoreverses = NO;
    rectRunAnimation.calculationMode = kCAAnimationLinear;
    rectRunAnimation.duration = 4;
    
    
    // 添加动画
    [rectLayer addAnimation:rectRunAnimation forKey:@"rectRunAnimation"];
}




@end
